﻿using System;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.IO;
using System.Net;
using TV_Organizer.Properties;

namespace TV_Organizer.EPGuides
{
    class EPGuidesConnector
    {
        #region Singleton pattern
        private EPGuidesConnector()
        {
            this.Initialize();
        }

        private static EPGuidesConnector m_instance = new EPGuidesConnector();
        public static EPGuidesConnector Instance
        {
            get { return m_instance; }
        }
        #endregion

        #region props
        public ObservableCollection<EPGuidesShow> TVShows { get; private set; }
        #endregion

        private void Initialize()
        {
            if (Settings.Default.EPGuidesShows == null)
                Settings.Default.EPGuidesShows = new System.Collections.Specialized.StringCollection();

            this.TVShows = new ObservableCollection<EPGuidesShow>();
            this.UpdateTVShows(Settings.Default.EPGuidesShows);

            Settings.Default.SettingsSaving += new System.Configuration.SettingsSavingEventHandler(Default_SettingsSaving);
        }

        void Default_SettingsSaving(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Settings.Default.EPGuidesShows.Clear();
            foreach (var tvShow in this.TVShows)
                Settings.Default.EPGuidesShows.Add(tvShow.SerializedValue);
        }

        private void UpdateTVShows(StringCollection lines)
        {
            var l_startDate = DateTime.Now;
            LogsManager.Log(this, "Parsing TV Shows - STARTED");

            this.TVShows.Clear();

            foreach (var line in lines)
            {
                try
                {
                    this.TVShows.Add(EPGuidesShow.CreateFromSerializedValue(line));
                }
                catch
                {
                    LogsManager.Log(this, string.Format("Could not parse following line: {0}", line));
                }
            }

            var l_endDate = DateTime.Now;
            LogsManager.Log(this, string.Format("Parsing TV Shows - FINISHED - took {0} ms", (l_endDate - l_startDate).TotalMilliseconds));
        }

        public void UpdateTVShows()
        {
            var l_startDate = DateTime.Now;
            LogsManager.Log(this, "Fetching EP Guides TV Shows list - STARTED");

            var l_client = new WebClient();
            var l_stream = l_client.OpenRead(Constants.AllShowsTXTUrl);
            var l_streamReader = new StreamReader(l_stream);
            // first line is description 
            l_streamReader.ReadLine();

            var l_lines = new StringCollection();

            string l_line = l_streamReader.ReadLine();
            while (l_line != null)
            {
                l_lines.Add(l_line);
                l_line = l_streamReader.ReadLine();
            }

            var l_endDate = DateTime.Now;
            LogsManager.Log(this, string.Format("Fetching EP Guides TV Shows list - FINISHED - took {0} ms", (l_endDate - l_startDate).TotalMilliseconds));

            this.UpdateTVShows(l_lines);

            l_stream.Close();
        }
    }
}
